package gov.pbc.nn.kjgl.xkyyh.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import gov.pbc.nn.core.dao.AbstractHibernateDao;
import gov.pbc.nn.kjgl.entity.WorkPlan;
import gov.pbc.nn.kjgl.item.template.entity.AcceptDept;
import gov.pbc.nn.kjgl.xkyyh.entity.BackTrain;
import gov.pbc.nn.kjgl.xkyyh.entity.NewBank;
import gov.pbc.nn.kjgl.xkyyh.entity.TrainDept;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.bstek.bdf2.core.context.ContextHolder;
import com.bstek.dorado.data.provider.Criteria;
import com.bstek.dorado.data.provider.Page;

@Repository
public class NewBankDao extends AbstractHibernateDao {

	public NewBank getByNewBankId(String id) {
		String hql = "from NewBank where invalid=false and id=:id";
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", id);
		List<NewBank> pf = this.query(hql, map);
		if (pf.isEmpty()) {
			return null;
		} else {
			return (NewBank) pf.get(0);
		}
	}

	public List<TrainDept> getTrainDepts(String id) {
		String hql = "from " + TrainDept.class.getName() + " where bankId=:id and invalid=false";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", id);
		return this.query(hql, params);
	}

	public BackTrain getBackTrain(String id) {
		String hql = "from " + BackTrain.class.getName() + " where trainId=:id and invalid=false";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", id);
		List<BackTrain> query = this.query(hql, params);
		if(query.size()>0){
			return query.get(0);
		}else{
			return new BackTrain();
		}
	}

	public void getNewBankPage(Page<NewBank> page, Criteria criteria) {
		DetachedCriteria query = this.buildDetachedCriteria(criteria, NewBank.class);
		query.add(Restrictions.or(Restrictions.eq("invalid", false), Restrictions.isNull("invalid")));
		// query.add(Restrictions.eq("createUser",
		// ContextHolder.getLoginUser().getUsername()));
		this.pagingQuery(page, query);
	}
}
